<template>
  <div class="produ">
    
    <div class="custom-tree-container">
      <div class="block">
        
        <el-tree
          :data="datas"
          :show-checkbox="false"
          empty-text="正在加载"
          node-key="id"
          :default-expand-all="false"
          :expand-on-click-node="false"
        >
          <span class="custom-tree-node" slot-scope="{ node, data }">
            <span>{{ node.label }}</span>
            <span style="float: right">
              <el-button type="text" size="mini" @click="() => append(data)">
                添加分类
              </el-button>
              <el-button
                type="text"
                size="mini"
                @click="() => remove(node, data)"
              >
                删除当前
              </el-button>
            </span>
          </span>
        </el-tree>
      </div>
    </div>
  </div>
</template>

<script>
import { produhttp } from "../../http/requset";
let id = 1000;
export default {
  created() {
    produhttp().then((res) => {
      this.arrlist = res.data.data;
      console.log(this.arrlist);
      this.huoqu()
      // console.log(this.newarrlist);
    });
  },
  mounted() {},

  computed: {},
  data() {
    const data = [
      {
        id: 1,
        label: "彩电",
        children: [
          {
            id: 4,
            label: "二级 1-1",
            children: [
              {
                id: 9,
                label: "三级 1-1-1",
              },
              {
                id: 10,
                label: "三级 1-1-2",
              },
            ],
          },
        ],
      },
      {
        id: 2,
        label: "一级 2",
        children: [
          {
            id: 5,
            label: "二级 2-1",
          },
          {
            id: 6,
            label: "二级 2-2",
          },
        ],
      },
      {
        id: 3,
        label: "一级 3",
        children: [
          {
            id: 7,
            label: "二级 3-1",
          },
          {
            id: 8,
            label: "二级 3-2",
          },
        ],
      },
    ];
    // this.datas = data;

    return {
      arrlist: [],
      datas: [],

      // datas: JSON.parse(JSON.stringify(this.datas)),
    };
  },
  methods: {
    append(data) {
      console.log(data);
      const newChild = { id: id++, label: "testtest", children: [] };
      if (!data.children) {
        this.$set(data, "children", []);
      }
      data.children.push(newChild);
    },

    remove(node, data) {
      console.log(node,data);
      const parent = node.parent;
      const children = parent.data.children || parent.data;
      const index = children.findIndex((d) => d.id === data.id);
      children.splice(index, 1);
    },

    renderContent(h, { node, data, store }) {
      return (
        <span class="custom-tree-node">
          <span>{node.label}</span>
          <span>
            <el-button
              size="mini"
              type="text"
              on-click={() => this.append(data)}
            >
              Append
            </el-button>
            <el-button
              size="mini"
              type="text"
              on-click={() => this.remove(node, data)}
            >
              Delete
            </el-button>
          </span>
        </span>
      );
    },
    huoqu() {
      
      const newarrlist = this.arrlist.map((r) => {
        return {
          id: r.cat_id,
          label: r.cat_name,
          children: r.children.map((r) => {
            if (r.children) {
              return {
                id: r.cat_id,
                label: r.cat_name,
                children: r.children.map((r) => {
                  if (r.children) {
                    return {
                      id: r.cat_id,
                      label: r.cat_name,
                    };
                  } else {
                    return {
                      id: r.cat_id,
                      label: r.cat_name,
                    };
                  }
                }),
              };
            } else {
              return {
                id: r.cat_id,
                label: r.cat_name,
              };
            }
          }),
        };
      });
      this.datas=newarrlist
    },
  },
};
</script>

<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
.produ{
height: 100%;
overflow: auto;
}
</style>